package com.ethink.platform.job;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ethink.report.common.mapper.MateInventoryAgeMapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Component
public class MateInventoryAgeJob {

    @Autowired
    private MateInventoryAgeMapper mateInventoryAgeMapper;

    @XxlJob("MateInventoryAgeJob")
    @Transactional(rollbackFor = Exception.class)
    public void run() {
        log.info("====================== MateInventoryAgeJob start ======================");
        String repDate = XxlJobHelper.getJobParam();
        if (StringUtils.isBlank(repDate)) {
            Date now = new Date();
            repDate = DateUtil.format(DateUtil.offsetDay(now, -1), DatePattern.PURE_DATE_FORMAT);
        }
        String month = repDate.substring(0, 6);
        mateInventoryAgeMapper.deleteHistory(month, repDate);
        mateInventoryAgeMapper.batchInsert(month, repDate);
        mateInventoryAgeMapper.updateF0t30(month, repDate);
        mateInventoryAgeMapper.updateF30t60(month, repDate);
        mateInventoryAgeMapper.updateF60t90(month, repDate);
        mateInventoryAgeMapper.updateF90t180(month, repDate);
        mateInventoryAgeMapper.updateF180t270(month, repDate);
        mateInventoryAgeMapper.updateF270t360(month, repDate);
        mateInventoryAgeMapper.updateF360(month, repDate);
        mateInventoryAgeMapper.updateAverageQty(month, repDate);
        log.info("====================== MateInventoryAgeJob end ======================");
    }

}
